#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>

int main()
{
	//create socket
	int sockFd = socket(PF_INET,SOCK_STREAM,0);
	if(sockFd < 0)
	{
		perror("create socket error!");
		return -1;
	}
	printf("create socket OK!\n");
	//请求连接
	struct sockaddr_in servAddr = {0};
	servAddr.sin_family = PF_INET;
	servAddr.sin_port = htons(6666);
	servAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	int ret = connect(sockFd,(struct sockaddr *)&servAddr,sizeof(servAddr));
	if(ret < 0)
	{
		perror("connect error!");
		close(sockFd);
		return -1;
	}
	printf("connect OK!\n");
	//write
	printf("write to server:");
	char buf[1024];//字节流要一样大，服务器端设置为1024，这里也要是1024
	while(1)
	{
		memset(buf,0,sizeof(buf));
		fgets(buf, 1024, stdin);
		ret = write(sockFd,buf,sizeof(buf));
		if(ret < 0)
		{
			perror("write error!");
			close(sockFd);
			return -1;
		}
		printf("write OK!\n");
		if(strncmp(buf,"quit",4) == 0)
			break;
	}
	//close
	close(sockFd);
	return 0;
}
